home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 12051 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.5 KB  |  59 lines

  1. Path: nntp-server.caltech.edu!news
  2. From: Xuhua Li <xuhua@cco.caltech.edu>
  3. Newsgroups: comp.lang.c++
  4. Subject: How to simulate EOF from the keyboard in Visual C++ 4.0, CTRL+Z doesn't work well
  5. Date: Sun, 17 Mar 1996 17:15:19 -0800
  6. Organization: California Institute of Technology, Pasadena
  7. Message-ID: <314CB927.22A3@cco.caltech.edu>
  8. NNTP-Posting-Host: xuhua-ppp.caltech.edu
  9. Mime-Version: 1.0
  10. Content-Type: text/plain; charset=gb2312
  11. Content-Transfer-Encoding: 7bit
  12. X-Mailer: Mozilla 2.0GoldB1 (Win95; I)
  13. CC: cpp-students@zib-berlin.de
  14.  
  15. The title says everything. When I try to simulate the EOF from the 
  16. keyboard in Visual C++ with CTRL+Z, it doesn't work. CTRL+Z just kill 
  17. the running program. I am running windows 95.
  18. Please try the following simple program from <<C++ Primer Plus>>:
  19.  
  20. Program #1
  21. // textin3.cpp -- reading chars to end of file
  22. #include <iostream.h>
  23. int main(void)
  24. {
  25.     char ch;
  26.     int count = 0;
  27.  
  28.     while (cin.get(ch))      // cin.get(ch) is 0 on EOF
  29.     {
  30.         cout << ch;
  31.         count++;
  32.     }
  33.     cout << count << " characters read\n";
  34.     return 0;
  35. }
  36.  
  37. Program #2
  38.  
  39. // ifelse.cpp -- using the if else statement
  40. #include <iostream.h>
  41. int main(void)
  42. {
  43.     char ch;
  44.  
  45.     cout << "Type, and I shall repeat.\n";
  46.     while (cin.get(ch))
  47.     {
  48.          if (ch == '\n')
  49.             cout << ch;     // done if newline
  50.          else
  51.             cout << ++ch;   // done otherwise
  52.     }
  53.     // try ch + 1 instead of ++ch for interesting effect
  54.     cout << "Please excuse the slight confusion.\n";
  55.     return 0;
  56. }
  57.  
  58. You assistance will be highly appreciated.
  59.